package gw.com.android.ui.account;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.gwtsz.gts2.hx.R;

/**
 * 登陆页使用，自定义下划线
 *
 * @author jett
 * @since 2018-01-17.
 */
public class LoginEditText extends android.support.v7.widget.AppCompatEditText {

    private CharSequence hint;
    private Paint paint = new Paint();
    private Rect mRect = new Rect();
    private int underLineWidth;
    private OnFocusChangeListener listener;

    public LoginEditText(Context context) {
        super(context);
        init();
    }

    public LoginEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LoginEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint.setAntiAlias(true);
        paint.setColor(getResources().getColor(R.color.color_d));
        underLineWidth = 1;//DeviceUtil.instance().dip2px(0.7F, getContext());
        super.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    paint.setColor(getResources().getColor(R.color.color_k));
                    hint = getHint();
                    setHint(null);
                } else {
                    paint.setColor(getResources().getColor(R.color.color_d));
                    if (!TextUtils.isEmpty(hint)){
                        setHint(hint);
                    }
                }
                if (listener != null) {
                    listener.onFocusChange(v, hasFocus);
                }
                postInvalidate();
            }
        });
    }

    @Override
    public void setOnFocusChangeListener(OnFocusChangeListener l) {
        this.listener = l;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRect.left = 0;
        mRect.top = h - underLineWidth;
        mRect.right = w;
        mRect.bottom = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), paint);
        canvas.drawRect(mRect, paint);
        super.onDraw(canvas);
    }

}
